﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Skin2D.Display;
using Skin2D.Bone;
using Skin2D.Control;
using Skin2D.Control.LogSystem;

namespace Skin2D
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Resize += new EventHandler(Form1_Resize);
            this.Load += new EventHandler(Form1_Load);
            this.splitContainer1.Resize += new EventHandler(splitContainer1_Resize);
            Timer timer = new Timer();
            timer.Interval = 1000 / 60;
            timer.Tick += new EventHandler(Refresh);
            timer.Start();
        }

        void Form1_Load(object sender, EventArgs e)
        {
            DisplayDriver.Get().Init(this.pictureBox1.Width, this.pictureBox1.Height);
            DisplayDriver.Get().InitCamera(this.pictureBox1);
            CoordinateDraw coord_draw = new CoordinateDraw();
            DisplayDriver.Get().AddDrawHandler(coord_draw);

            Bone2D root = new Bone2D(null);
            root.setTranslate(DisplayDriver.Get().Width / 2, DisplayDriver.Get().Height / 2);
            root.setRotate(-30);
            root.UpdateAbsolute();
            Bone2D child_1 = new Bone2D(root);
            child_1.setTranslate(root.Length, 0);
            child_1.setRotate(0);
            root.AddChild(child_1);

            Bone2D child_2 = new Bone2D(root);
            child_2.setTranslate(0, 0);
            child_2.setRotate(-90);
            root.AddChild(child_2);

            BoneDraw bone_draw = new BoneDraw(root);
            DisplayDriver.Get().AddDrawHandler(bone_draw);

            InitViewControls(root);
            
        }

        void InitViewControls(Bone2D bone)
        {
            MyApplication.m_root = bone;

            MyApplication.m_BoneListTreeView = new CtrBoneListTreeView(this.splitContainerProperty.Panel1, bone);

            MyApplication.m_PropertyGrid = new CtrPropertyGrid(this.splitContainerProperty.Panel2);

            MyApplication.m_BoneListTreeView.OnChangeSelected += MyApplication.m_PropertyGrid.SetData;

            MyApplication.m_CursorManager = new CursorManager(pictureBox1);
            
            MyApplication.m_CursorManager.InitCursor(this.selectButton,CurrsorType.Select);

            MyApplication.m_CursorManager.InitCursor(this.resizeButton, CurrsorType.Resize);

            MyApplication.m_CursorManager.InitCursor(this.rotateButton, CurrsorType.Rotate);

            MyApplication.m_CursorManager.setCursorType(CurrsorType.Select);
        }

        void Refresh(object sender, EventArgs e)
        {
            DisplayDriver.Get().Clear();
            DisplayDriver.Get().Draw();
            this.pictureBox1.Image = DisplayDriver.Get().GetImages();
        }

        void Form1_Resize(object sender, EventArgs e)
        {
            DisplayDriver.Get().Resize(this.pictureBox1.Width, this.pictureBox1.Height);
        }

        void splitContainer1_Resize(object sender, EventArgs e)
        {
            DisplayDriver.Get().Resize(this.pictureBox1.Width, this.pictureBox1.Height);
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EventLog.Undo();
        }

        private void toolStripUndo_Click(object sender, EventArgs e)
        {
            EventLog.Undo();
        }

        private void moveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyApplication.m_CursorManager.setCursorType(CurrsorType.Select);
        }

        private void rotateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyApplication.m_CursorManager.setCursorType(CurrsorType.Rotate);
        }

        private void resizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyApplication.m_CursorManager.setCursorType(CurrsorType.Resize);
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            MyApplication.AddBone();
        }

        private void toolStripRemove_Click(object sender, EventArgs e)
        {
            MyApplication.RemoveBone();
        }

    }
}
